<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class FrontendController extends CController
{
    /**
     * false ： no use layouts views
     * @var bool
     */
	public $layout=false;

	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();

	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

    public $_option = array();

	public $_baseUrl;
	public $_basePath;
	public $_request;
	public $_themeUrl;
	public $_data;
	public $_resourceFrontend;
    public $_websiteTitle;
    public $_seoTitle;
    public $_seoKeywords;
    public $_seoDescription;
    public $_websiteUrl;
    public $_website_icp;
    public $_telephone;
    public $_fax;
    public $_email;
    public $_address;
    public $_postcode;
    public $_navMenu;
    public $_commonNav = ''; //菜单导航
    public $_currentUrl = ''; //当前URL

    /**
     * init extension parent
     */
    public function init()
    {
        parent::init();
        //Language select
        if(isset($_GET['lang']) && $_GET['lang'] != "") {
            Yii::app()->language = $_GET['lang'];
            Yii::app()->request->cookies['lang'] = new CHttpCookie('lang', $_GET['lang']);
        } else if(!empty(Yii::app()->request->cookies['lang'])) {
            Yii::app()->language = Yii::app()->request->cookies['lang'];
        } else {
            $lang = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            Yii::app()->language = strtolower(str_replace('-', '_', $lang[0]));
        }

        //系统配置
        $this->_baseUrl       = Yii::app()->baseUrl;
        $this->_basePath      = Yii::app()->basePath;
        $this->_request       = Yii::app()->request;
        $this->_themeUrl      = $this->_baseUrl.'/themes/default/';
        $this->_data          = ROOT_PATH.'/protected/data/';
        $this->_resourceFrontend = $this->_baseUrl.'/resource/frontend';

        //后台全局设置
        $options = Options::model()->findAll();
        foreach ($options as $row) {
            $this->_option[$row['name']] = $row['value'];
        }

        //$this->_conf = XCache::system('_options');
        //$this->_category = Category::get(0, XCache::system('_category'));
        $this->_websiteTitle = $this->_option['website_title'];
        $this->_seoTitle = $this->_option['seo_title'];
        $this->_seoKeywords = $this->_option['seo_keywords'];
        $this->_seoDescription = $this->_option['seo_description'];
        $this->_websiteUrl = $this->_option['website_url'];
        $this->_website_icp = $this->_option['website_icp'];
        if($this->_option['website_status'] == 0)
            self::_closed();
        //后台自定义字段中添加配置
        if(isset($this->_option['telephone']))
            $this->_telephone = $this->_option['telephone'];
        if(isset($this->_option['fax']))
            $this->_fax = $this->_option['fax'];
        if(isset($this->_option['email']))
            $this->_email = $this->_option['email'];
        if(isset($this->_option['address']))
            $this->_address = $this->_option['address'];
        if(isset($this->_option['postcode']))
            $this->_postcode = $this->_option['postcode'];


        //菜单导航
        $menus = Menu::model()->findAll("type='1' AND is_display='1' order by sort ASC, id ASC");
        $tree = new Xtree();
        foreach((array)$menus as $menu){
            $data[] = $menu->attributes;
        }
        $tree->setTree($data, 'id', 'parent_id', array('name','url','target'));
        $this->_commonNav = (array)$tree->getArrayList(0);
        //var_dump($this->_commonNav);
        $this->_currentUrl = Yii::app()->request->getUrl();
        //var_dump($this->_currentUrl);
    }

    /**
     * 前台导航菜单
     */
    public function FrontendNav(){
        $nav = Menu::model()->findAll("type='1' AND is_display='1' order by sort ASC, id ASC");
        $navList = Menu::getMenuTree(0, $nav);
        return $navList;
    }

    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'fontFile' =>  ROOT_PATH.'/resource/backend/fonts/Britanic.ttf', //自定义字体
                'height' => 20,
                'width'  => 80,
                'padding'=>0,
                'minLength' => 4,
                'maxLength' => 4,
                //'backColor'=>0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page'=>array(
                'class'=>'CViewAction',
            ),
        );
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

}